package com.finger.forest;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.finger.forest.adapter.HotCheckListAdapter;
import com.finger.forest.adapter.MyAdapter;
import com.finger.forest.base.BaseActivity;
import com.finger.forest.base.BaseView;
import com.finger.forest.base.CommonKey;
import com.finger.forest.base.ResponseTag;
import com.finger.forest.http.presenter.RegisterPresenter;
import com.finger.forest.model.FzHotspot;
import com.finger.forest.utils.ClickUtils;
import com.finger.forest.utils.encode.PrefsUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 热点列表（已反馈、未反馈）
 */
public class HotListActivity extends BaseActivity<RegisterPresenter> implements BaseView {

    private static final String TAG = "HotListActivity";

    /**
     * 返回
     */
    @BindView(R.id.btn_back)
    ImageView backIv;

    //声明ViewPager
    private ViewPager mViewpager;

    //声明ViewPager的适配器
    private PagerAdapter mAdpater;

    //用于装载两个Tab的List
    private List<View> mTabs = new ArrayList<View>();

    /**
     * 已反馈
     */
    @BindView(R.id.tv_nav_check)
    TextView navCheckTv;

    /**
     * 未反馈
     */
    @BindView(R.id.tv_nav_uncheck)
    TextView navUnCheckTv;

    private SmartRefreshLayout hotCheckSmart;
    private SmartRefreshLayout hotUnCheckSmart;
    private List<FzHotspot> hotFeedAdapterList = new ArrayList<>();
    private List<FzHotspot> hotUnFeedAdapterList = new ArrayList<>();
    private HotCheckListAdapter hotCheckListAdapter;
    private HotCheckListAdapter hotUnCheckListAdapter;

    @Override
    protected int setView() {
        return R.layout.activity_hot_list;
    }

    @Override
    protected void receiveData() {

    }

    @Override
    protected void initializeView() {

    }

    private void queryList(){
        String userId = PrefsUtil.getString(this, CommonKey.USER_ID);
        Map<String, Object> maps = new HashMap<>();
        maps.put("recordby",userId);
        presenter.queryHotList(maps);//查询热点核查列表
    }

    private void initHotList(){
        initViews();
        initEvents();
        initDatas();
    }

    @Override
    protected void loadDataFromServer() {
        initHotList();
    }

    //初始化控件
    private void initViews() {
        mViewpager = (ViewPager) findViewById(R.id.id_viewpager_hot);

        //获取到两个Tab
        LayoutInflater inflater = LayoutInflater.from(this);
        View tab1 = inflater.inflate(R.layout.tab_hot_refresh1, null);
        initTab1(tab1, hotFeedAdapterList);
        View tab2 = inflater.inflate(R.layout.tab_hot_refresh2, null);
        initTab2(tab2, hotUnFeedAdapterList);

        //将两个Tab添加到集合中
        mTabs.add(tab1);
        mTabs.add(tab2);
    }

    /**
     * 初始化Tab1
     * @param tab
     */
    private void initTab1(View tab, List<FzHotspot> list) {
        hotCheckSmart = tab.findViewById(R.id.srlSmart);
        hotCheckSmart.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNUm++;
                isRefresh = true;
                queryList();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNUm = 1;
                isRefresh = true;
                queryList();
            }
        });
        hotCheckSmart.autoRefresh();

        RecyclerView rvCollection = tab.findViewById(R.id.rvCollection);

        hotCheckListAdapter = new HotCheckListAdapter(R.layout.item_hot, list, HotListActivity.this, 1);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rvCollection.setLayoutManager(linearLayoutManager);
        rvCollection.setAdapter(hotCheckListAdapter);
    }

    /**
     * 初始化Tab2
     * @param tab
     */
    private void initTab2(View tab, List<FzHotspot> list) {
        hotUnCheckSmart = tab.findViewById(R.id.srlSmart);
        hotUnCheckSmart.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNUm++;
                isRefresh = true;
                queryList();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNUm = 1;
                isRefresh = true;
                queryList();
            }
        });
        hotUnCheckSmart.autoRefresh();

        RecyclerView rvCollection = tab.findViewById(R.id.rvCollection);

        hotUnCheckListAdapter = new HotCheckListAdapter(R.layout.item_hot, list, HotListActivity.this,0);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rvCollection.setLayoutManager(linearLayoutManager);
        rvCollection.setAdapter(hotUnCheckListAdapter);
    }

    private void initEvents() {
        //添加ViewPager的切换Tab的监听事件
        mViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //获取ViewPager的当前Tab
                int currentItem = mViewpager.getCurrentItem();
                //将所以的TextView设置成黑色
                reseNavColor();
                //将当前Tab对应的TextView设置成蓝色
                switch (currentItem) {
                    case 0:
                        navCheckTv.setTextColor(getResources().getColor(R.color.colorBlue));
                        break;
                    case 1:
                        navUnCheckTv.setTextColor(getResources().getColor(R.color.colorBlue));
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void reseNavColor() {
        navCheckTv.setTextColor(getResources().getColor(R.color.black));
        navUnCheckTv.setTextColor(getResources().getColor(R.color.black));
    }

    private void initDatas() {
        //初始化ViewPager的适配器
        mAdpater = new PagerAdapter() {
            @Override
            public int getCount() {
                return mTabs.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View view = mTabs.get(position);
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(mTabs.get(position));
            }
        };
        //设置ViewPager的适配器
        mViewpager.setAdapter(mAdpater);
    }

    @OnClick({R.id.btn_back, R.id.tv_nav_check, R.id.tv_nav_uncheck })
    public void onClick(View v) {
        if (ClickUtils.isFastClick()) { //防止重复点击
            switch (v.getId()) {

                //返回
                case R.id.btn_back:
                    finish();
                    break;

                case R.id.tv_nav_check:
                    mViewpager.setCurrentItem(0);
                    break;

                case R.id.tv_nav_uncheck:
                    mViewpager.setCurrentItem(1);
                    break;

                default:
                    break;
            }
        }
    }

    @Override
    public void onSuccess(Object model, int tag, String msg) {

        switch (tag) {

            //初始化热点核查
            case ResponseTag.QUERY_HOT_LIST:
                List<FzHotspot> hotList = (List<FzHotspot>) model;
                List<FzHotspot> hotFeedList = new ArrayList<>();
                List<FzHotspot> hotUnFeedList = new ArrayList<>();
                for (FzHotspot hotO:hotList){
                    if (hotO.gethHostpotStatus() == 0){
                        hotUnFeedList.add(hotO);//未反馈
                    } else {
                        hotFeedList.add(hotO);//已反馈
                    }
                }
                if (null != hotList) {
                    if (isRefresh) {
                        hotFeedAdapterList.clear();
                        hotUnFeedAdapterList.clear();
                    }
                    hotFeedAdapterList.addAll(hotFeedList);
                    hotUnFeedAdapterList.addAll(hotUnFeedList);
                }
                hotCheckListAdapter.notifyDataSetChanged();
                hotUnCheckListAdapter.notifyDataSetChanged();

                finishCheckSmart();
                finishUnCheckSmart();
                break;

            default:
                break;
        }
    }

    @Override
    public void onError(int errcode, int tag, String message) {

    }

    private void finishCheckSmart() {
        if (null != hotCheckSmart) {
            hotCheckSmart.finishRefresh();
            hotCheckSmart.finishLoadMore();
        }
    }

    private void finishUnCheckSmart() {
        if (null != hotUnCheckSmart) {
            hotUnCheckSmart.finishRefresh();
            hotUnCheckSmart.finishLoadMore();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent datat) {
        switch(requestCode){
            case 1:
                //返回默认显示已反馈
                mViewpager.setCurrentItem(0);
                queryList();
                hotCheckListAdapter.notifyDataSetChanged();
                hotUnCheckListAdapter.notifyDataSetChanged();
                Log.i(TAG,"返回刷新");
        }

        super.onActivityResult(requestCode, resultCode, datat);
    }

}
